package com.dk.assertion;

import com.alibaba.fastjson.JSON;
import com.dk.domain.ApiAutoTestEntity;
import com.dk.domain.assertion.CheckPointEntity;
import com.dk.enumerate.HttpEnum;
import com.dk.util.http.res.ResUtil;
import com.dk.util.log.Log;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 空检查断言
 */
public class NullCheckLink extends AssertChain{
    /**
     * 空检查断言实现
     * @param apiAutoTestEntity 检查点对象
     * @return
     */
    @Override
    public ApiAutoTestEntity assertBody(ApiAutoTestEntity apiAutoTestEntity) {
        //最终返回结果
        AtomicBoolean result = new AtomicBoolean(true);
        //获取检查点对象
        CheckPointEntity checkPointEntity = JSON.parseObject(apiAutoTestEntity.getCheckPoint(),CheckPointEntity.class);
        if(checkPointEntity.getNullCheck()!=null&&checkPointEntity.getNullCheck()){
            String body = apiAutoTestEntity.getResBody();
            Log.info("---【用例断言】:开始空检查断言");
            if(body.contains(HttpEnum.EMPTY_NULL.getValue())||body.contains(HttpEnum.EMPTY_OBJECT.getValue())||body.contains(HttpEnum.EMPTY_ARRAY.getValue())){
                result.set(false);
                Log.error("---【用例断言】:断言失败，当前响应体包含空数据");
            }
            Log.info("---【用例断言】:空检查断言结束");
        }
        apiAutoTestEntity.setAssertResult(result.get());
        return apiAutoTestEntity;
    }
}
